const joi = require('joi');

const arrayString = ['banana','bacon','cheese'];
const arrayObjects = [{example:'example1'},{example:'example2'},{example:'example3'}];

const userInput = {personalInfo:{
                        streetAddress:'123987789',
                        city:'Sydney',
                        state:'NSW'
                    },
                    preferences1:arrayString,
                    preferences2:arrayObjects
                };

const personalInfoSchema = joi.object().keys({
    streetAddress:joi.string().trim().required(),
    city:joi.string().trim().required(),
    state:joi.string().trim().length(3).required()
});

const preferencesSchema1 = joi.array().items(joi.string());

const preferencesSchema2 = joi.array().items(joi.object().keys({
    example:joi.string().required()
}));

const shema = joi.object().keys({
    personalInfo:personalInfoSchema,
    preferences1:preferencesSchema1,
    preferences2:preferencesSchema2
});

joi.validate(userInput,shema,(err,result)=>{
    if(err) console.log(err);
    else console.log(result);
});